<template>
  <div class="return-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>归还管理</span>
        </div>
      </template>
      
      <el-form :model="returnForm" :rules="rules" ref="returnFormRef" label-width="100px">
        <el-form-item label="图书ID" prop="bookId">
          <el-input v-model="returnForm.bookId" placeholder="请输入图书ID" />
        </el-form-item>
        <el-form-item label="归还人" prop="returner">
          <el-input v-model="returnForm.returner" placeholder="请输入归还人姓名" />
        </el-form-item>
        <el-form-item label="归还日期" prop="returnDate">
          <el-input
            v-model="returnForm.returnDate"
            type="date"
            placeholder="选择归还日期"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleReturn">确认归还</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card style="margin-top: 20px">
      <template #header>
        <div class="card-header">
          <span>归还记录</span>
        </div>
      </template>
      
      <el-table :data="returnRecords" style="width: 100%" v-loading="loading">
        <el-table-column prop="bookId" label="图书ID" width="80" />
        <el-table-column prop="title" label="书名" />
        <el-table-column prop="returner" label="归还人" />
        <el-table-column prop="returnDate" label="归还日期">
          <template #default="scope">
            {{ formatDate(scope.row.returnDate) }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag type="success">
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import dayjs from 'dayjs'

const loading = ref(false)
const returnFormRef = ref(null)
const returnForm = ref({
  bookId: '',
  returner: '',
  returnDate: dayjs().format('YYYY-MM-DD')
})

const rules = {
  bookId: [{ required: true, message: '请输入图书ID', trigger: 'blur' }],
  returner: [{ required: true, message: '请输入归还人姓名', trigger: 'blur' }],
  returnDate: [{ required: true, message: '请选择归还日期', trigger: 'change' }]
}

const returnRecords = ref([])

const formatDate = (date) => {
  if (!date) return ''
  return date
}

const handleReturn = async () => {
  if (!returnFormRef.value) return
  
  await returnFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        const formData = {
          ...returnForm.value,
          returnDate: formatDate(returnForm.value.returnDate),
          title: '示例图书',
          status: '已归还'
        }
        returnRecords.value.push(formData)
        ElMessage.success('归还成功')
        resetForm()
      } catch (error) {
        ElMessage.error('归还失败')
      }
    }
  })
}

const resetForm = () => {
  if (returnFormRef.value) {
    returnFormRef.value.resetFields()
  }
}
</script>

<style scoped>
.return-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style> 